package request

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"strings"
	"time"

	"golang.org/x/exp/rand"
	"spider.com/day/settings"
)


func sendReq(uuid string, client *http.Client, days string, code string, proxy string) (*settings.OneResponse, error) {

	client.Timeout = 5 * time.Second
	req, err := http.NewRequest("GET", fmt.Sprintf(settings.StockOneLineUrl, code), nil)
	if err != nil {
		return nil, err
	}
	// if reqID, ok := ctx.Value("uuidKey").(string); ok {
	//     fmt.Println("Generated UUID:", reqID)
	// } else {
	//     fmt.Println("UUID not found in context")
	// }

	req.Header.Set("User-Agent", settings.Headers[rand.Intn(len(settings.Headers))])
	req.Header.Set("Cookie", settings.Cookies[0])

	if proxy != "" {
		// proxyURL, err := url.Parse("http://127.0.0.1:8080")
		proxyURL, err := url.Parse(proxy)
		if err != nil {
			log.Println(uuid, "Error proxy")
			return nil, err
		}
		transport := &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
		}
		client.Transport = transport
	}

	res, err := client.Do(req)
	if err != nil {
		log.Println(uuid, "Error request")
		return nil, err
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		log.Println(uuid, "Error IO ReadAll")
		return nil, err
	}

	if res.StatusCode != 200 {
		return nil, err
	}
	var response settings.OneResponse
	// err = json.NewDecoder(res.Body).Decode(&response)
	b := strings.Replace(string(body), "quotepushdata88(", "", 1)
	c := strings.Replace(b, "quotepushdata2(", "", 1)
	d := strings.Replace(c, ");", "", 1)
	err = json.Unmarshal([]byte(d), &response)
	if err != nil {
		log.Println(uuid, "Error Unmarshal", d)
		return nil, err
	}

	return &response, nil
}

func SendSnowReq(uuid string, client *http.Client, days string, code string, proxy string, ts int64) (*settings.SnowResponse, error) {

	client.Timeout = 5 * time.Second
	c := fmt.Sprintf(settings.StockOneLineSnowUrl, code, ts)
	req, err := http.NewRequest("GET", c, nil)
	if err != nil {
		return nil, err
	}
	// if reqID, ok := ctx.Value("uuidKey").(string); ok {
	//     fmt.Println("Generated UUID:", reqID)
	// } else {
	//     fmt.Println("UUID not found in context")
	// }

	req.Header.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
	req.Header.Add("accept-language", "zh-CN,zh;q=0.9")
	req.Header.Add("cache-control", "max-age=0")
	req.Header.Add("cookie", "")
	req.Header.Add("priority", "u=0, i")
	req.Header.Add("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"")
	req.Header.Add("sec-ch-ua-mobile", "?0")
	req.Header.Add("sec-ch-ua-platform", "\"macOS\"")
	req.Header.Add("sec-fetch-dest", "document")
	req.Header.Add("sec-fetch-mode", "navigate")
	req.Header.Add("sec-fetch-site", "none")
	req.Header.Add("sec-fetch-user", "?1")
	req.Header.Add("upgrade-insecure-requests", "1")
	req.Header.Add("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")

	req.Header.Set("user-agent", settings.Headers[rand.Intn(len(settings.Headers))])
	req.Header.Set("cookie", settings.Cookies[0])


	if proxy != "" {
		// proxyURL, err := url.Parse("http://127.0.0.1:8080")
		proxyURL, err := url.Parse(proxy)
		if err != nil {
			log.Println(uuid, "Error proxy")
			return nil, err
		}
		transport := &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
		}
		client.Transport = transport
	}

	res, err := client.Do(req)
	if err != nil {
		log.Println(uuid, "Error request")
		return nil, err
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		log.Println(uuid, "Error IO ReadAll")
		return nil, err
	}

	if res.StatusCode != 200 {
		return nil, err
	}
	var response settings.SnowResponse

	err = json.Unmarshal(body, &response)
	if err != nil {
		log.Println(uuid, "Error Unmarshal", string(body))
		return nil, err
	}

	return &response, nil
}